﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_ThongTinSinhVien : System.Web.UI.Page
{
    DataClassesDataContext data = new DataClassesDataContext();
    HocVien hv = new HocVien();
    int action;
    string mssv = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            PageLoad();
        }
        else
        {
            //LoadThongTin(Request.QueryString["MSSV"].ToString()); //Lỗi : Không thay đổi giá trị textbox
        }
    }

    private void PageLoad()
    {
        action = int.Parse(Request.QueryString["action"]);
        if (action == 1)
        { 
        }
        if (action == 2)
        {
            mssv = Request.QueryString["MSSV"].ToString();
            if (mssv != "")
                LoadThongTin(Request.QueryString["MSSV"].ToString());
            else
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Error !!!Không có mã số sinh viên');", true);
                Response.Redirect("./QuanLyHocVien.aspx");
            }
        }
    }

    private void LoadThongTin(string p)
    {
        DataClassesDataContext data = new DataClassesDataContext();
        var svs = from temp in data.tbl_SINHVIENs
                 where temp.MsSinhVien == p
                 select temp;
        foreach (tbl_SINHVIEN sv in svs)
        {
            txtHoTenSV.Text = sv.TenSinhVien;
            txtLop.Text = sv.Lop;
            txtNgaySinh.Text = sv.NgaySinh.Value.ToShortDateString();
            txtDiaChi.Text = sv.DiaChi;
            txtEmail.Text =sv.Email;
            txtSoDT.Text = sv.SoDT;
            ddlGioiTinh.SelectedValue = sv.GioiTinh.Value.ToString();
        }
        txtMSSV.Text = p;
        txtMSSV.Enabled = false;
   }

    private string TuDongSinhMaHocVien()
    {
        string MaHV = "SV001";
        int a = 1;
        while (hv.KiemTraMaHV(MaHV) == true)
        {
            if (a < 9)
            {
                a++;
                MaHV = "SV00" + a;
            }
            else if (a >= 9 && a < 99)
            {
                a++;
                MaHV = "SV0" + a;
            }
            else if (a >= 99)
            {
                a++;
                MaHV = "SV" + a;
            }
        }
        return MaHV;
    }

    protected void btnSavePerson_Click(object sender, EventArgs e)
    {
        try
        {
            action = int.Parse(Request.QueryString["action"]);

            if (action == 1)
            {
                if (KiemtraMa(txtMSSV.Text.ToString()))
                {

                    tbl_SINHVIEN sv = new tbl_SINHVIEN();
                    sv.MsSinhVien = txtMSSV.Text.ToString();
                    sv.TenSinhVien = txtHoTenSV.Text.ToString();
                    sv.Lop = txtLop.Text.ToString();
                    sv.GioiTinh = bool.Parse(ddlGioiTinh.SelectedValue.ToString());
                    string hoi = txtNgaySinh.Text.ToString();
                    DateTime hoi1 = DateTime.Parse(hoi);
                    sv.NgaySinh = DateTime.Parse(txtNgaySinh.Text.ToString());
                    sv.DiaChi = txtDiaChi.Text.ToString();
                    sv.Email = txtEmail.Text.ToString();
                    sv.SoDT = txtSoDT.Text.ToString();

                    data.tbl_SINHVIENs.InsertOnSubmit(sv);
                    data.SubmitChanges();

                    tbl_DANGNHAP tk = new tbl_DANGNHAP();
                    tk.TenDangNhap = txtMSSV.Text;
                    tk.MatKhau = txtMSSV.Text;
                    tk.MaQuyen = "user";

                    data.tbl_DANGNHAPs.InsertOnSubmit(tk);
                    data.SubmitChanges();
                }
                else
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Error !!! Mã số sinh viên đã tồn tại');", true);
                }
            }
            if (action == 2)
            {
                mssv = Request.QueryString["MSSV"].ToString();

                DataClassesDataContext data = new DataClassesDataContext();

                var SuaSV = from p in data.tbl_SINHVIENs
                             where p.MsSinhVien == mssv
                             select p;


                //tbl_SINHVIEN Sua_SV = data.tbl_SINHVIENs.Single(p => p.MsSinhVien == mssv);
                foreach (tbl_SINHVIEN Sua_SV in SuaSV)
                {
                    Sua_SV.TenSinhVien = txtHoTenSV.Text.ToString();
                    Sua_SV.Lop = txtLop.Text.ToString();
                    Sua_SV.GioiTinh = bool.Parse(ddlGioiTinh.SelectedValue.ToString());
                    Sua_SV.NgaySinh = DateTime.Parse(txtNgaySinh.Text.ToString());
                    Sua_SV.DiaChi = txtDiaChi.Text.ToString();
                    Sua_SV.Email = txtEmail.Text.ToString();
                    Sua_SV.SoDT = txtSoDT.Text.ToString();
                }

                data.SubmitChanges(); 
            }
            Response.Redirect("./QuanLyHocVien.aspx");

        }
        catch (Exception ex)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Error !!!"+ex.Message.ToString()+"');", true);
        }

    }

    private bool KiemtraMa(string mssv)
    {
        var query = from p in data.tbl_SINHVIENs
                    where p.MsSinhVien == mssv
                    select p;
        if (query.Count() > 0)
            return false;
        else return true;
    }
}
